using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Deloitte.Data;
using Deloitte.Windows.Forms;
using Deloitte.Common;
namespace Deloitte.Windows.Controls
{
	/// <summary>
	/// Summary description for ControlSelectDistinct.
	/// </summary>
	public class DistinctColumnValuesControl : System.Windows.Forms.UserControl
	{
		private DataTable2 dataTable2;
        private DataTable dtResults;
		private System.Windows.Forms.ComboBox cboColumnName;
        private System.Windows.Forms.Label label1;
        private DataGridView dataGridView1;
        private Button getDisctinctButton;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem viewInDataFormToolStripMenuItem;
        private IContainer components;

		public DistinctColumnValuesControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call
			this.CommonConstructorCode();
		}
		public DistinctColumnValuesControl(DataTable table)
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			this.dataTable2 = (new DataTable2(table));
			this.CommonConstructorCode();
		}
		public DistinctColumnValuesControl(DataTable2 table)
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			this.dataTable2 = table;
			this.CommonConstructorCode();
		}
		private void CommonConstructorCode()
		{
            this.cboColumnName.Items.Clear();
            for (int i = 0; i < this.dataTable2.Columns.Count; i++) this.cboColumnName.Items.Add(this.dataTable2.Columns[i].ColumnName);
			this.dtResults = new DataTable();
			this.dtResults.Columns.Add("Distinct Values", typeof(string));
            this.dataGridView1.DataSource = this.dtResults;

		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            this.cboColumnName = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.getDisctinctButton = new System.Windows.Forms.Button();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.viewInDataFormToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // cboColumnName
            // 
            this.cboColumnName.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
            this.cboColumnName.Location = new System.Drawing.Point(96, 8);
            this.cboColumnName.Name = "cboColumnName";
            this.cboColumnName.Size = new System.Drawing.Size(256, 19);
            this.cboColumnName.TabIndex = 21;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(23, 11);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(65, 11);
            this.label1.TabIndex = 22;
            this.label1.Text = "Column Name";
            // 
            // dataGridView1
            // 
            dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
            this.dataGridView1.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
            this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.ContextMenuStrip = this.contextMenuStrip1;
            this.dataGridView1.Location = new System.Drawing.Point(16, 32);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(426, 118);
            this.dataGridView1.TabIndex = 24;
            // 
            // getDisctinctButton
            // 
            this.getDisctinctButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.getDisctinctButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.getDisctinctButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.getDisctinctButton.Location = new System.Drawing.Point(15, 151);
            this.getDisctinctButton.Name = "getDisctinctButton";
            this.getDisctinctButton.Size = new System.Drawing.Size(111, 23);
            this.getDisctinctButton.TabIndex = 30;
            this.getDisctinctButton.Text = "&Get Distinct Values";
            this.getDisctinctButton.UseVisualStyleBackColor = true;
            this.getDisctinctButton.Click += new System.EventHandler(this.getDisctinctButton_Click);
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.viewInDataFormToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(172, 26);
            // 
            // viewInDataFormToolStripMenuItem
            // 
            this.viewInDataFormToolStripMenuItem.Name = "viewInDataFormToolStripMenuItem";
            this.viewInDataFormToolStripMenuItem.Size = new System.Drawing.Size(171, 22);
            this.viewInDataFormToolStripMenuItem.Text = "View in Data Form";
            this.viewInDataFormToolStripMenuItem.Click += new System.EventHandler(this.viewInDataFormToolStripMenuItem_Click);
            // 
            // DistinctColumnValuesControl
            // 
            this.AutoScroll = true;
            this.Controls.Add(this.getDisctinctButton);
            this.Controls.Add(this.dataGridView1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.cboColumnName);
            this.Font = new System.Drawing.Font("Tahoma", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "DistinctColumnValuesControl";
            this.Size = new System.Drawing.Size(456, 176);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

        private void getDisctinctButton_Click(object sender, EventArgs e)
        {
            GetDisctinctColumnValues();
        }

        private void GetDisctinctColumnValues()
        {
            if (this.cboColumnName.Text != null)
            {
                this.dtResults.Rows.Clear();
                DataRow dr;
                string[] distinctVals = this.dataTable2.SelectDistinctColumnValues(this.cboColumnName.Text);
                foreach (string val in distinctVals)
                {
                    dr = this.dtResults.NewRow();
                    dr[0] = val;
                    this.dtResults.Rows.Add(dr);
                }
            }
        }

        private void viewInDataFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataForm dataForm = new DataForm(Utility.CreateDataTable(this.dataGridView1));
            dataForm.Show();
        }

	}
}
